<?php
	$nguoidung = new nguoidungbs();
	$danhsach = $nguoidung->GetList();
	
	$chucvu = new chucvubs();
	$dschucvu = $chucvu->GetList();
?>

<div id="NoiDung" class="form">
  <h3>DANH SÁCH NGƯỜI DÙNG</h3>
  <table class="table table-bordered">
    <thead>
      <tr>
        <th>STT</th>
        <th>Họ và tên</th>
        <th>Tên đăng nhập</th>
        <th>Chức vụ</th>
       
        <?php
		if($_SESSION['QuyenHan']==1 && isset( $_SESSION['QuyenHan']))
		echo' <th>Khóa</th>
		<th>Sửa</th>
		<th>Xóa</th>';
		?>
      </tr>
    </thead>
    <?php
		for($i = 0; $i < ($danhsach == false ? 0 : count($danhsach)); $i++)
		{
			echo "<input type='hidden' value=".$danhsach[$i]['MaNguoiDung']." name='MaNguoiDung[]' form='Luu' />";
		$stt=$i+1;
				echo "<tbody>
					<tr>";
					echo "<td>" . $stt. "</td>";
					echo "<td><a href = 'nguoidung-chitiet_" . $danhsach[$i]['MaNguoiDung'] ." '>" . $danhsach[$i]['TenNguoiDung'] . "</a></td>";
					echo "<td>" . $danhsach[$i]['TenDangNhap'] . "</td>";
					if( isset($_SESSION['QuyenHan']))
					{
						if($danhsach[$i]['MaNguoiDung'] != $_SESSION['MaNguoiDung'] && $_SESSION['QuyenHan'] == 1)
						{
							echo "<td>  <select name='MaChucVu[]' form='Luu'>";
								for($j=0;$j<count($dschucvu);$j++)
								{
									if($dschucvu[$j]['MaChucVu']==$danhsach[$i]['MaChucVu'])
										echo "<option value=".$dschucvu[$j]['MaChucVu']." selected='selected'>".$dschucvu[$j]['TenChucVu']."</option>";
									else
										echo "<option value=".$dschucvu[$j]['MaChucVu'].">".$dschucvu[$j]['TenChucVu']."</option>";
								}
							echo "</select>";
						}
					else
						echo "<td>". $danhsach[$i]['TenChucVu']."</td>";
					if($_SESSION['QuyenHan'] == 1)
						{
							echo "</td>";
							echo "<td align='center'>"; 
							if($danhsach[$i]['MaNguoiDung'] != $_SESSION['MaNguoiDung'])
							{
								if($danhsach[$i]['Khoa'] == 0)
									echo "<a href='nguoidung-kichhoat_1_" . $danhsach[$i]['MaNguoiDung'] . "'><span class='glyphicon glyphicon-remove-sign'></span></a>";
								else
									echo "<a href='nguoidung-kichhoat_0_" . $danhsach[$i]['MaNguoiDung'] . "'><span class='glyphicon glyphicon-ok-sign'></span></a>";
							}else
							{
								if($danhsach[$i]['Khoa'] == 0)
									echo "<span class='glyphicon glyphicon-remove-sign'></span>";
								else
									echo "<span class='glyphicon glyphicon-ok-sign'></span>";
							}
							echo "</td>";
							
							echo "<td align='center'><a href='nguoidung-sua_" . $danhsach[$i]['MaNguoiDung'] . "'><span class='glyphicon glyphicon-pencil'></span></a></td>";
							echo "<td align='center'><a href='nguoidung-xoa_" . $danhsach[$i]['MaNguoiDung'] . "' onclick='return confirm(\"Bạn có muốn xóa người dùng " . $danhsach[$i]['TenNguoiDung'] . " không?\")'><span class='glyphicon glyphicon-trash'></span></a></td>";
						}
					}else
						echo "<td>". $danhsach[$i]['TenChucVu']."</td>";
				echo "</tr>
				</tbody>";
			
		}
	?>
  </table>
  <?php
	if(isset($_SESSION['QuyenHan']) && $_SESSION['QuyenHan']==1)
	{ 
	echo '<form id="Luu" action="nguoidung" method="post">
			<input class="pull-right btn btn-primary btn-primary" type="submit" value="Lưu" name="btnLuu" />
		</form>';
	echo '  <a href="dangky">Thêm người dùng</a> ';
	}
?>
</div>
<?php
	if(isset($_POST['btnLuu']))
	{
		$data = new nguoidungbs();
		$MaChucVu=$_POST['MaChucVu'];
		$MaNguoiDung=$_POST['MaNguoiDung'];
		for($i=0;$i<count($MaNguoiDung);$i++)
		{
			$data->MaChucVu = $MaChucVu[$i];
			$data->MaNguoiDung = $MaNguoiDung[$i];
			$kq=$data->ChangeChucVu();
			if($kq)
			{
				header("Location: nguoidung");
				ThongBao('Lưu thành công');
			}
			else
			ThongBaLoi('Lỗi truy vấn');
		}
	}
?>
